//
//  DSZClassifyShopsViewController.m
//  7.13苏宁框架
//
//  Created by app20 on 15/7/18.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZClassifyShopsViewController.h"
#import "DSZLYViewController.h"
#import "DSZClassifyShopsModel.h"
#import "DSZShopsModel.h"
#import "DSZShopsCell.h"
#import "DSZShopsViewFlowLayout.h"
#import "DSZShopsCollectionCell.h"
#import "Common.h"
#import "DSZMainViewController.h"
#import "DSZSelecterMoreController.h"
#import "UIScrollView+MJRefresh.h"
#import "GoodSDetailModel.h"
#import "XQViewController.h"
#import "XQModel.h"
@interface DSZClassifyShopsViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
// 数据源
@property(nonatomic,strong) NSArray *classifyArray;
@property(nonatomic,strong) NSMutableArray *shopsArray;

// 搜索栏
@property(nonatomic,weak) UITextField *navTextFiled;

// 导航栏浏览模式变换按钮
@property(nonatomic,weak) UIButton *navChangeViewBtn;

// 导航栏曲线按钮
@property(nonatomic,weak) UIButton *navCancelBtn;

// 上一次点击的分类视图按钮
@property(nonatomic,weak) UIButton *lastClickClassifyBtn;

// 分类按钮选中背景视图
@property(nonatomic,weak) UIView *BGView;

// 分类按钮视图
@property(nonatomic,weak) UIView *classifyBtnView;

// 筛选按钮视图
@property(nonatomic,weak) UIView *selscterBarView;


// 热词视图
@property(nonatomic,weak) UIScrollView *TFHotWordsHistoryView;

// 分类表浏览视图
@property(nonatomic,weak) UITableView *listView;

// 分类九宫格浏览视图
@property(nonatomic,weak) UICollectionView *shopsCollectionView;

// 热词历史纪录按钮Y值
@property(nonatomic,assign) CGFloat btnY;

// 返回顶部
@property(nonatomic,weak) UIButton *toTopBtn;


@property(nonatomic,strong)NSArray *array;
@end



@implementation DSZClassifyShopsViewController

- (void)requestData{
    
    NSArray *result = [WXNetworkService classifyData];
    
    _classifyArray=[NSArray array];
    _classifyArray=[DSZClassifyShopsModel modelArrayWithDictArray:result];
    
    NSArray *result1 = [WXNetworkService shopsData];
    
    _shopsArray=[NSMutableArray arrayWithArray:[DSZShopsModel modelArrayWithDictArray:result1]];
    
}


-(void)viewWillAppear:(BOOL)animated
{
    // 加载搜索框
    [self loadTextFiled];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 加载数据
    [self requestData];
    
    // 加载搜索框
    [self loadTextFiled];
    
    // 加载分类按钮
    [self loadClassifyBtns];
    
    // 加载筛选栏
    [self loadSelectBar];
    
    // 加载列表视图
    [self loadListView ];
    
    // 加载九宫格视图
    [self loadCollectionView];
    
    // 隐藏分栏控制器
    DSZMainViewController *main=(DSZMainViewController *)self.tabBarController;
    [main hiddenMainTabbar:YES];
    
    
    // 添加返回顶部按钮
    UIButton *top = [UIButton buttonWithType:UIButtonTypeSystem];
    top.frame = CGRectMake(self.view.width - 50, self.view.height - 50, 25, 25);
    [top setBackgroundImage:[UIImage imageNamed:@"top.jpg"] forState:UIControlStateNormal];
    [top addTarget:self action:@selector(clickTotop) forControlEvents:UIControlEventTouchUpInside];
    top.hidden = YES;
    _toTopBtn = top;
    [self.view addSubview:top];
    
    
}



- (void)loadTextFiled{
    
    // 自定义导航栏视图
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 270, 44)];
    
    UIBarButtonItem *customLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navView];
    
    self.navigationItem.rightBarButtonItem = customLeftBarButtonItem;
    
    
    
    // 添加改变浏览模式按钮
    UIButton *navChangeViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(240, 7, 30, 30)];
    [navChangeViewBtn setBackgroundImage:[UIImage imageNamed:@"collectionView.png"] forState:UIControlStateNormal];
    [navChangeViewBtn setBackgroundImage:[UIImage imageNamed:@"plistView.png"] forState:UIControlStateSelected];
    
    [navChangeViewBtn addTarget:self action:@selector(clickChangeViewBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [navView addSubview:navChangeViewBtn];
    _navChangeViewBtn = navChangeViewBtn;
    
    // 添加取消按钮
    UIButton *navCancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(235, 7, 40, 30)];
    navCancelBtn.font = [UIFont systemFontOfSize:14];
    [navCancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [navCancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [navCancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [navCancelBtn addTarget:self action:@selector(clickCancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:navCancelBtn];
    navCancelBtn.hidden = YES;
    _navCancelBtn = navCancelBtn;
    
    // 添加搜索框
    UITextField *navTextFielfd = [[UITextField alloc] initWithFrame:CGRectMake(0, 7, 230, 30)];
    navTextFielfd.borderStyle = UITextBorderStyleRoundedRect;
    navTextFielfd.placeholder = @"三星A8 震撼首发";
    navTextFielfd.font = [UIFont systemFontOfSize:15];
    navTextFielfd.clearButtonMode = UITextFieldViewModeWhileEditing;
    navTextFielfd.returnKeyType = UIReturnKeySearch;
    navTextFielfd.delegate = self;
    _navTextFiled = navTextFielfd;
    [navView addSubview:navTextFielfd];
    
    // 给搜索框添加左辅助视图
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 2.5, 40, 25)];
    [leftBtn setTitle:@"商品" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn setTitle:@"店铺" forState:UIControlStateSelected];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    leftBtn.font = [UIFont systemFontOfSize:14];
    
    [leftBtn addTarget:self action:@selector(clickLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btnDownArrow_icon.png"]];
    rightImageView.frame = CGRectMake(leftBtn.frame.size.width + 5, leftBtn.frame.size.height / 2 , 10, 5);
    [leftView addSubview:leftBtn];
    [leftView addSubview:rightImageView];
    
    navTextFielfd.leftView = leftView;
    navTextFielfd.leftViewMode = UITextFieldViewModeAlways;
    
    
    // 给搜索框添加右辅助视图
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 15)];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, rightView.frame.size.width - 8, rightView.frame.size.height )];
    
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"homeSearchTool_voiceIcon.png"] forState:UIControlStateNormal];
    
    [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [rightView addSubview:rightBtn];
    
    navTextFielfd.rightView = rightView;
    navTextFielfd.rightViewMode = UITextFieldViewModeAlways;
    
    
    
}

// 加载分类按钮
- (void)loadClassifyBtns{
    
    //  添加背景
    UIView *classifyBtnsView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, 44)];
    _classifyBtnView = classifyBtnsView;
    [self.view addSubview:classifyBtnsView];
    
    UIView *segmentation = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.view.width, 0.5)];
    segmentation.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [classifyBtnsView addSubview:segmentation];
    
    UIView *BGView = [[UIView alloc] initWithFrame:CGRectMake(0, 41, self.view.width / 4, 3)];
    BGView.backgroundColor = [UIColor orangeColor];
    [classifyBtnsView addSubview:BGView];
    _BGView = BGView;
    
    
    NSArray *btnTitles = @[@"综合",@"销量",@"价格",@"评价"];
    
    CGFloat btnx = 0;

    for (int i = 0; i < 4; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(btnx, 0, self.view.width / 4, classifyBtnsView.height);
        [classifyBtnsView addSubview:btn];
        
        [btn setTitle:btnTitles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        btn.tag = i;
        
        if (i == 0) {
            
            btn.font = [UIFont systemFontOfSize:15];
            
            _lastClickClassifyBtn = btn;
            
        }else{
            
            btn.font = [UIFont systemFontOfSize:12];
        }
        
        [btn addTarget:self action:@selector(clickClassifyBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        btnx += btn.width;
    }
}

// 加载筛选栏
- (void)loadSelectBar{

    //  添加背景视图
    UIView *SelectBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 108, self.view.width, 44)];
    _selscterBarView = SelectBarView;
    [self.view addSubview:SelectBarView];
    
    UIView *segmentation = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.view.width, 0.5)];
    segmentation.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [SelectBarView addSubview:segmentation];
    
    // 添加筛选按钮视图
    UIView *selecterView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width - 85, 0, 85, SelectBarView.height)];
    [SelectBarView addSubview:selecterView];
    
    UIView *segmentationV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.5, 44)];
    segmentationV.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [selecterView addSubview:segmentationV];
    
    UIButton *selecteMorerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    selecteMorerBtn.frame = CGRectMake(0, 0, 70, selecterView.height);
    [selecteMorerBtn setTitle:@"更多筛选" forState:UIControlStateNormal];
    [selecteMorerBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    selecteMorerBtn.font = [UIFont systemFontOfSize:12];
    [selecteMorerBtn addTarget:self action:@selector(clickSelecterMoreBtn) forControlEvents:UIControlEventTouchUpInside];
    [selecterView addSubview:selecteMorerBtn];
    
    UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(65, 15, 10, 14)];
    arrow.image = [UIImage imageNamed:@"arrow_right.png"];
    [selecterView addSubview:arrow];
    
    
    // 添加筛选滚动按钮视图
    UIScrollView *selecterScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width - selecterView.width, 44)];
    [SelectBarView addSubview:selecterScrollView];
    
    NSArray *selectBtns = @[@"品牌",@"空调类型",@"商品匹数"];
    
    CGFloat btnX = 15;
    
    for (int i = 0; i < selectBtns.count; i++) {
        
        NSString *title = selectBtns[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(btnX , 9, title.length * 20 + 10, 25);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"btnBack.png"] forState:UIControlStateNormal];
        [selecterScrollView addSubview:btn];
        
        btnX += btn.width + 10;
    }
    
    selecterScrollView.contentSize = CGSizeMake(btnX + 20, 44);
    
    selecterScrollView.showsHorizontalScrollIndicator = NO;
    
}

// 添加分类列表视图
- (void)loadListView{

    UITableView *listTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 152, self.view.width, self.view.height - 152) style:UITableViewStylePlain];
    listTV.dataSource = self;
    listTV.delegate = self;
    
    // 添加上拉刷新
    listTV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(UpRefresh)];
    
    _listView = listTV;
//    [_listView addFooterWithTarget:self action:@selector(UpRefresh)];
    [self.view addSubview:listTV];
}

// 加载九宫格视图
//static只能自己使用 const常量值不能修改
static NSString *const ID = @"shopsCollectionCell";
- (void)loadCollectionView{
    
    UICollectionView *shopsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 152, self.view.width, self.view.height - 64 - 49) collectionViewLayout:[[DSZShopsViewFlowLayout alloc] init]];
    
    shopsCollectionView.dataSource = self;
    shopsCollectionView.delegate = self;
    
    [shopsCollectionView registerNib:[UINib nibWithNibName:@"DSZShopsCollectionCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
    shopsCollectionView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    
    shopsCollectionView.hidden = YES;
    
    // 上拉刷新
    shopsCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(UpRefresh)];
    
    _shopsCollectionView = shopsCollectionView;
    
    [self.view addSubview:shopsCollectionView];

}



// 加载搜索框正在编辑搜索历史视图
- (void)loadTFHotWordsHistoryView{
    
    // 添加背景视图
    UIScrollView *TFHotWordsHistoryView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.size.width, self.view.size.height - 64 - 216)];
    TFHotWordsHistoryView.backgroundColor = [UIColor colorWithRed:0.90 green:0.90 blue:0.90 alpha:1];
    TFHotWordsHistoryView.delegate = self;
    TFHotWordsHistoryView.directionalLockEnabled = YES;
    TFHotWordsHistoryView.scrollEnabled = YES;
    TFHotWordsHistoryView.showsVerticalScrollIndicator = YES;
    TFHotWordsHistoryView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    
    // 初始化数据
    CGFloat marginLeft = 10;
    CGFloat marginTop = 15;
    CGFloat tipW = 60;
    CGFloat tipH = 30;
    CGFloat extend = 3;
    CGFloat strLong = 20;
    CGFloat btnX = marginLeft;
    _btnY = marginTop;
    CGFloat btnH = 25;
    CGFloat btnSpace = 10;
    CGFloat rowSpace = 10;
    
    // 添加人们推荐标签
    UILabel *hotWordsTip = [[UILabel alloc] initWithFrame:CGRectMake(btnX, _btnY, tipW, tipH)];
    hotWordsTip.backgroundColor = [UIColor clearColor];
    hotWordsTip.text = @"热门推荐";
    hotWordsTip.font = [UIFont systemFontOfSize:15];
    hotWordsTip.textColor = [UIColor grayColor];
    [TFHotWordsHistoryView addSubview:hotWordsTip];
    
    _btnY = _btnY + tipH + rowSpace;
    
    for (int i = 0; i < [[_classifyArray[0] hotwords] count]; i++) {
        
        NSString *title = [[_classifyArray[0] hotwords][i] objectForKey:@"hotword"];
        NSInteger numOfStr = title.length;
        CGFloat btnW = extend * 2 + strLong * numOfStr;
        
        if (btnX + btnW > TFHotWordsHistoryView.width - marginLeft - 10) {
            
            btnX = marginLeft;
            _btnY = _btnY + btnH + rowSpace;
        }
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, _btnY, btnW, btnH)];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.font = [UIFont systemFontOfSize:14];
        btn.backgroundColor = [UIColor whiteColor];
        
        [btn addTarget:self action:@selector(didClickHotWordsBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [TFHotWordsHistoryView addSubview:btn];
        
        btnX = btnX + btnW + btnSpace;
    }
    
    btnX = marginLeft;
    _btnY = _btnY + btnH + marginTop;
    
    _TFHotWordsHistoryView = TFHotWordsHistoryView;
    
    [self.view addSubview:TFHotWordsHistoryView];
    
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    _navChangeViewBtn.hidden = YES;
    _navCancelBtn.hidden = NO;
    
    [self didClickHotWordsBtn:nil];
    
    return YES;
}

#pragma mark - UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _shopsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * ID = @"cell";
    
    DSZShopsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [[DSZShopsCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:ID];
        
    }
    cell.icon = [_shopsArray[indexPath.row] icon];
    cell.title = [_shopsArray[indexPath.row] title];
    cell.describe = [_shopsArray[indexPath.row] describe];
    cell.price = [_shopsArray[indexPath.row] price];
    cell.commentNum = [_shopsArray[indexPath.row] commentNum];

    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}// 返回行高

-(NSArray*)array
{
    _array = [NSArray array];
    _array = [GoodSDetailModel modelArrayWithFilename:@"GoodSDetail.plist"];
    return _array;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodSDetailModel *model = self.array[indexPath.row % 18];
    XQModel *xqmodel = [[XQModel alloc]init];
    xqmodel.image = model.image;
    xqmodel.name = model.name;
    xqmodel.price = model.price;
    xqmodel.oldprice = model.oldprice;
    xqmodel.know = model.know;
    xqmodel.fuwu = model.fuwu;
    xqmodel.hpl = model.hpl;
    xqmodel.person = model.person;
    xqmodel.pj = model.pj;
    xqmodel.detailpj = model.detailpj;
    xqmodel.smallimage = model.smallimage;
    xqmodel.type = model.type;
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"XQ" bundle:nil];
    XQViewController *con = (XQViewController*)[story instantiateViewControllerWithIdentifier:@"xq"];
    con.model = xqmodel;
    [self.navigationController pushViewController:con animated:YES];

    
}// 选中某行

#pragma mark - UIScrollViewDelehgate
static int lastOffset = 0;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint contentOffset = scrollView.contentOffset;
    
    if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < scrollView.contentSize.height - scrollView.height + 20) {
        
        if (scrollView.contentOffset.y - lastOffset > 10) {
            
            if (_classifyBtnView.top == 64) {
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.5];
                self.navigationController.navigationBar.frame = CGRectMake(0, -64, 320, 64);
                _classifyBtnView.frame = CGRectMake(0, -24, 320, 64);
                _selscterBarView.frame = CGRectMake(0, 20, 320, 64);
                scrollView.frame = CGRectMake(0, 64, 320, self.view.height - 64);
                [UIView commitAnimations];
                
                
//                scrollView.contentOffset = contentOffset;
            }
            
            
        }else if(scrollView.contentOffset.y - lastOffset < -5){
            
            if (_classifyBtnView.top == -24) {
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.5];
                self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 64);
                _classifyBtnView.frame = CGRectMake(0, 64, 320, 64);
                _selscterBarView.frame = CGRectMake(0, 108, 320, 64);
                scrollView.frame = CGRectMake(0, 152, 320, self.view.height - 152);
                [UIView commitAnimations];
                
//                
//                scrollView.contentOffset = contentOffset;
            }
         
        }
        
        lastOffset = scrollView.contentOffset.y;
        
    }
    
    if (scrollView.contentOffset.y > 1000) {
        
        if (_toTopBtn.hidden) {
            
            _toTopBtn.hidden = NO;
        }
       
    }else{
    
        if (!_toTopBtn.hidden) {
            
            _toTopBtn.hidden = YES;
        }
    }
    
}// 监听表的滑动


#pragma mark - UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return _shopsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DSZShopsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.icon = [_shopsArray[indexPath.row] icon];
    cell.title = [_shopsArray[indexPath.row] title];
    cell.price = [_shopsArray[indexPath.row] price];
    
    return cell;
    
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodSDetailModel *model = self.array[indexPath.row % 18];
    XQModel *xqmodel = [[XQModel alloc]init];
    xqmodel.image = model.image;
    xqmodel.name = model.name;
    xqmodel.price = model.price;
    xqmodel.oldprice = model.oldprice;
    xqmodel.know = model.know;
    xqmodel.fuwu = model.fuwu;
    xqmodel.hpl = model.hpl;
    xqmodel.person = model.person;
    xqmodel.pj = model.pj;
    xqmodel.detailpj = model.detailpj;
    xqmodel.smallimage = model.smallimage;
    xqmodel.type = model.type;
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"XQ" bundle:nil];
    XQViewController *con = (XQViewController*)[story instantiateViewControllerWithIdentifier:@"xq"];
    con.model = xqmodel;
    [self.navigationController pushViewController:con animated:YES];

}


#pragma mark - clickButtons
- (void)didClickHotWordsBtn:(UIButton *)btn{
    
    [_TFHotWordsHistoryView removeFromSuperview];
    [self loadTFHotWordsHistoryView];
    
    // 解归档，获取历史纪录数组
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"historyBtns.data"];
    
    NSMutableArray *historyBtns = [NSMutableArray array];
    
    //  如果解归档为空，不做操作
    if ([NSKeyedUnarchiver unarchiveObjectWithFile:filePath]) {
        
        historyBtns = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    }
    
    // 如果btn为空 不做操作，不为空，复制按钮并放入历史数组
    if (btn) {
        
        UIButton *historyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [historyBtn setTitle:btn.titleLabel.text forState:UIControlStateNormal];
        [historyBtn setTitleColor:btn.titleLabel.textColor forState:UIControlStateNormal];
        historyBtn.frame = btn.frame;
        
        historyBtn.backgroundColor = btn.backgroundColor;
        
        [historyBtns insertObject:historyBtn atIndex:0];
        
        if (historyBtns.count > 5) {
            
            [historyBtns removeLastObject];
        }
        
        [NSKeyedArchiver archiveRootObject:historyBtns toFile:filePath];
        
    }
    
    CGFloat marginLeft = 10;
    CGFloat btnX = marginLeft;
    CGFloat btnSpace = 10;
    CGFloat rowSpace = 10;
    CGFloat tipW = 60;
    CGFloat tipH = 30;
    
    if (btn || [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]) {
        
        // 添加历史记录标签
        UILabel *historyTip = [[UILabel alloc] initWithFrame:CGRectMake(btnX, _btnY, tipW, tipH)];
        historyTip.backgroundColor = [UIColor clearColor];
        historyTip.text = @"历史记录";
        historyTip.font = [UIFont systemFontOfSize:15];
        historyTip.textColor = [UIColor grayColor];
        [_TFHotWordsHistoryView addSubview:historyTip];
        
        _btnY = _btnY + tipH + rowSpace;
    }
    
    for (UIButton *subBtn in historyBtns) {
        
        if (btnX + subBtn.width > _TFHotWordsHistoryView.width - marginLeft * 2) {
            
            btnX = marginLeft;
            
            _btnY += subBtn.height + rowSpace;
        }
        
        subBtn.frame = CGRectMake(btnX, _btnY, subBtn.width, subBtn.height);
        subBtn.font = [UIFont systemFontOfSize:14];
        [_TFHotWordsHistoryView addSubview:subBtn];
        
        [subBtn addTarget:self action:@selector(didClickHotWordsBtn:) forControlEvents:UIControlEventTouchUpInside];
        btnX = btnX + subBtn.width + btnSpace;
    }
    
    // 添加清除历史按钮
    _btnY += btn.height + 40;
    marginLeft += 10;
    CGFloat cleanHistoryBtnH = 30;
    
    if (historyBtns.count) {
        
        UIButton *cleanHistoryBtn = [[UIButton alloc] initWithFrame:CGRectMake(marginLeft, _btnY, _TFHotWordsHistoryView.width - marginLeft *2, cleanHistoryBtnH)];
        [cleanHistoryBtn setBackgroundImage:[UIImage imageNamed:@"btnBack.png"] forState:UIControlStateNormal];
        [cleanHistoryBtn setTitle:@"清除历史" forState:UIControlStateNormal];
        [cleanHistoryBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [cleanHistoryBtn addTarget:self action:@selector(didClickClearHistoryBtn) forControlEvents:UIControlEventTouchUpInside];
        [_TFHotWordsHistoryView addSubview:cleanHistoryBtn];
        _btnY += cleanHistoryBtnH * 4;
    }
    
    _TFHotWordsHistoryView.contentSize = CGSizeMake(self.view.width, _btnY);
}



// 点击搜索框右边麦克风按钮
-(void)clickRightBtn{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"DSZLYViewController" bundle:nil];
    DSZLYViewController *vc=[story instantiateViewControllerWithIdentifier:@"lv"];
    [self presentViewController:vc animated:YES completion:nil];
}

// 点击搜索框左边按钮
- (void)clickChangeViewBtn:(UIButton *)btn{

    if ([btn isSelected]) {
        
        btn.selected = NO;
        _shopsCollectionView.hidden = YES;
        
    }else{
    
        btn.selected = YES;
        _shopsCollectionView.hidden = NO;
    }
}

// 点击导航栏取消按钮
-(void)clickCancelBtn{

    _navCancelBtn.hidden = YES;
    _navChangeViewBtn.hidden = NO;
    
    _navTextFiled.text = nil;
    [_navTextFiled endEditing:YES];
    
    [_TFHotWordsHistoryView removeFromSuperview];
}

// 点击分类按钮
- (void)clickClassifyBtn:(UIButton *)btn{
    
    _lastClickClassifyBtn.font = [UIFont systemFontOfSize:12];
    btn.font = [UIFont systemFontOfSize:15];
    _lastClickClassifyBtn = btn;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    _BGView.frame = CGRectMake(self.view.width / 4 * btn.tag, 41, self.view.width / 4, 3);
    
    [UIView commitAnimations];

}

- (void)clickSelecterMoreBtn{
    
    [self performSegueWithIdentifier:@"more" sender:nil];

}

// 点击返回顶部按钮
- (void)clickTotop{

    _listView.contentOffset = CGPointMake(0, 0);
}

// 上拉刷新
- (void)UpRefresh{


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [_shopsArray addObjectsFromArray:_shopsArray];
        
        [_listView reloadData];
        [_shopsCollectionView reloadData];
        //[_listView footerEndRefreshing];
        //[_shopsCollectionView footerEndRefreshing];
        
        [_listView.footer endRefreshing];
        [_shopsCollectionView.footer endRefreshing];
        
    });
    
}

//// 按钮高亮无效
//- (void)setHighlighted:(BOOL)highlighted {}

@end
